import requests
import json
import time
albummid='003dAEX43IElBh'
# 替换为你的实际专辑接口地址
album_url = f"http://localhost:3300/album/songs?albummid={albummid}"
# 替换为你的获取播放地址接口模板
play_url_template = "http://localhost:3300/song/url?id={song_mid}"

response = requests.get(album_url)
data = response.json()

# 提取专辑信息
album_data = data.get("data", {})
album_mid = album_data.get("albummid", "")
song_list = album_data.get("list", [])
album_name = song_list[0].get("album", {}).get("name", "未知专辑") if song_list else "未知专辑"

# 构造歌曲信息列表
songs = []
for song in song_list:
    song_name = song.get("name", "")
    song_mid = song.get("mid", "")

    # 获取歌手列表
    singers = song.get("singer", [])
    singer_objs = [
        {
            "singer_id": s.get("mid", ""),
            "singer_name": s.get("name", "")
        }
        for s in singers
    ]

    # 获取播放地址
    try:
        play_url_req = requests.get(play_url_template.format(song_mid=song_mid))
        play_data = play_url_req.json()
        play_url = play_data.get("data", "")
    except Exception as e:
        play_url = ""
        print(f"获取 {song_name} 播放地址失败: {e}")

    # 添加到歌曲列表中
    songs.append({
        "song_name": song_name,
        "song_mid": song_mid,
        "play_url": play_url,
        "singers": singer_objs
    })

    # 可选：为了避免服务请求过快，延迟一点
    time.sleep(0.1)

# 最终 JSON 结构
result = {
    "album_name": album_name,
    "album_mid": album_mid,
    "songs": songs
}

# 输出结果
print(json.dumps(result, indent=2, ensure_ascii=False))